package com.yaozheng.vocationaltraining.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.bean.PKCategoryBean;
import com.yaozheng.vocationaltraining.bean.PKRankBean;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;

import java.util.List;

/**
 * Created by moon on 2017/11/14.
 */

public class PKRankAdapter extends BaseAdapter {
    private LayoutInflater mInflater;//
    private Activity mContext;
    private BitmapLoader userBitmapLoader;

    private List<PKRankBean> data = null;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    private int userId;

    private int selectedPosition = -1;

    public PKRankAdapter(Activity context,
                             List<PKRankBean> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.data = data;
        userBitmapLoader = new BitmapLoader(Glide.with(context), R.drawable.user_img);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(
                    R.layout.pk_rank_list_item, null);
            holder = new ViewHolder();

            holder.root_layout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            holder.userAvatarImage = (ImageView) convertView
                    .findViewById(R.id.userAvatarImage);
            holder.name_textview = (TextView) convertView
                    .findViewById(R.id.name_textview);
            holder.rank_serial_textview = (TextView) convertView
                    .findViewById(R.id.rank_serial_textview);

            holder.position_textview = (TextView) convertView
                    .findViewById(R.id.position_textview);
            holder.store_name_textview = (TextView) convertView
                    .findViewById(R.id.store_name_textview);
            holder.win_rate_textview = (TextView) convertView
                    .findViewById(R.id.win_rate_textview);
            holder.percent_tip_textview = (TextView) convertView.findViewById(R.id.percent_tip_textview);


            convertView.setTag(holder);//
        } else {
            holder = (ViewHolder) convertView.getTag();//
        }

        //加载图片
        userBitmapLoader.displayImage(data.get(position).getAvatar(), holder.userAvatarImage);
        holder.name_textview.setText(data.get(position).getName());

        if (position == 0){
            holder.rank_serial_textview.setBackgroundResource(R.drawable.rank_1);
        }else if (position == 1){
            holder.rank_serial_textview.setBackgroundResource(R.drawable.rank_2);
        }else if (position == 2){
            holder.rank_serial_textview.setBackgroundResource(R.drawable.rank_3);
        }else {
            holder.rank_serial_textview.setBackground(null);
            holder.rank_serial_textview.setText(position+"");
        }
        if(!TextUtils.isEmpty(data.get(position).getPosition()) && !"null".equals(data.get(position).getPosition())){
            holder.position_textview.setVisibility(View.VISIBLE);
            holder.position_textview.setText(data.get(position).getPosition());
        }else {
            holder.position_textview.setVisibility(View.GONE);
        }

        holder.store_name_textview.setText(data.get(position).getStoreName());

        holder.win_rate_textview.setText(data.get(position).getRate()/100.0+"");


        if (position == 0){
            holder.root_layout.setBackground(mContext.getResources().getDrawable(R.drawable.pk_rank_first_bg_color));
        }else if (position == 1){
            holder.root_layout.setBackground(mContext.getResources().getDrawable(R.drawable.pk_rank_second_bg_color));
        }else if (position == 2){
            holder.root_layout.setBackground(mContext.getResources().getDrawable(R.drawable.pk_rank_three_bg_color));
        }else {
            holder.root_layout.setBackgroundColor(mContext.getResources().getColor(R.color.default_bg_color));
        }

        if(userId == data.get(position).getUid()){ //如果是自己，则字体颜色为红色
            holder.rank_serial_textview.setTextColor(mContext.getResources().getColor(R.color.footer_bg_color));
            holder.name_textview.setTextColor(mContext.getResources().getColor(R.color.footer_bg_color));
            holder.position_textview.setTextColor(mContext.getResources().getColor(R.color.footer_bg_color));
            holder.store_name_textview.setTextColor(mContext.getResources().getColor(R.color.footer_bg_color));

            holder.win_rate_textview.setTextColor(mContext.getResources().getColor(R.color.footer_bg_color));
            holder.percent_tip_textview.setTextColor(mContext.getResources().getColor(R.color.footer_bg_color));

        }else if(position == 0 || position == 1 || position == 2){
            holder.rank_serial_textview.setTextColor(mContext.getResources().getColor(R.color.default_bg_color));
            holder.name_textview.setTextColor(mContext.getResources().getColor(R.color.default_bg_color));
            holder.position_textview.setTextColor(mContext.getResources().getColor(R.color.default_bg_color));
            holder.store_name_textview.setTextColor(mContext.getResources().getColor(R.color.default_bg_color));

            holder.win_rate_textview.setTextColor(mContext.getResources().getColor(R.color.default_bg_color));
            holder.percent_tip_textview.setTextColor(mContext.getResources().getColor(R.color.default_bg_color));
        }else {
            holder.rank_serial_textview.setTextColor(mContext.getResources().getColor(R.color.black));
            holder.name_textview.setTextColor(mContext.getResources().getColor(R.color.black));
            holder.position_textview.setTextColor(mContext.getResources().getColor(R.color.black));
            holder.store_name_textview.setTextColor(mContext.getResources().getColor(R.color.black));

            holder.win_rate_textview.setTextColor(mContext.getResources().getColor(R.color.black));
            holder.percent_tip_textview.setTextColor(mContext.getResources().getColor(R.color.black));
        }

        return convertView;
    }

    public void setData(List<PKRankBean> data,int userId) {
        this.data = data;
        this.userId = userId;
    }

    public void setselectedPosition(int selectedPosition) {
        notifyDataSetChanged();;
    }

    class ViewHolder {
        ImageView userAvatarImage;
        TextView name_textview;
        TextView rank_serial_textview;
        TextView position_textview;
        TextView store_name_textview;
        TextView win_rate_textview;
        TextView percent_tip_textview;

        LinearLayout root_layout;
    }
}
